package com.ruoyi.hospital.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.framework.datasource.DynamicDataSourceContextHolder;
import com.ruoyi.hospital.domain.HPatient;
import com.ruoyi.hospital.domain.HPatientPdf;
import com.ruoyi.hospital.domain.ViewCpjsPatlist;
import com.ruoyi.hospital.mapper.HPatientPdfMapper;
import com.ruoyi.hospital.service.IHpatientPdfService;
import com.ruoyi.hospital.service.IHpatientService;
import com.ruoyi.hospital.service.IViewCpjsPatlistService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 患者pdf serviceimpl
 * @author liz
 * @date 2023-10-18 10:37
 */
@Service
public class HPatientPdfServiceImpl extends ServiceImpl<HPatientPdfMapper, HPatientPdf> implements IHpatientPdfService {

    @Autowired
    private IViewCpjsPatlistService viewCpjsPatlistService;

    @Autowired
    IHpatientPdfService hpatientPdfService;

    @Autowired
    IHpatientService hpatientService;

    /**
     * 根据inpatientNo获取患者pdf文件
     */
    @Override
    public HPatientPdf selectPdfByInpatientNo(String id) {
        DynamicDataSourceContextHolder.setDataSourceType(DataSourceType.SLAVE.name());
        ViewCpjsPatlist viewCpjsPatlist = viewCpjsPatlistService.getPdfByInpatientNo(id);
        DynamicDataSourceContextHolder.clearDataSourceType();
        if(viewCpjsPatlist.getJsdPdf() != null){
            // 保存患者pdf
            HPatientPdf hPatientPdf = new HPatientPdf();
            hPatientPdf.setJsdPdf(viewCpjsPatlist.getJsdPdf());
            hpatientPdfService.save(hPatientPdf);
            // 更新患者pdfid
            UpdateWrapper updateWrapper = new UpdateWrapper<HPatient>();
            updateWrapper.set("jsd_pdf_id",Convert.toStr(hPatientPdf.getId()));
            updateWrapper.eq("inpatient_no",id);
            hpatientService.update(updateWrapper);
            return hPatientPdf;
        } else {
            return null;
        }
    }
}
